package com.zhixuan.autoui.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import cd.zhixuan.utils.recycler.RecyclerGridSelectTextView;
import cd.zhixuan.utils.recycler.adapter.BaseRecyclerAdapter;
import cd.zhixuan.utils.recycler.adapter.RecyclerListAdapter;
import cd.zhixuan.utils.recycler.adapter.listener.OnBindHolder;
import cd.zhixuan.utils.recycler.adapter.listener.OnItemClickListener;
import com.zhixuan.autoui.R;
import com.zhixuan.autoui.widget.DialogSpeechRecognition;

import java.util.List;

import cd.zhixuan.utils.dialog.Toaster;

/**
 * author: HuZC
 * Date: 2021/11/26
 * Description:
 */
public class MainActivity extends Activity {

    private RecyclerGridSelectTextView<String> rgstView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.sample_activity_main);

        rgstView = findViewById(R.id.rgst_view);
        rgstView.getLayoutManager().setSpanCount(4);

        RecyclerListAdapter<String> adapter = rgstView.getAdapter();
        adapter.setDataList(new String[]{"音量设置", "背光设置", "语音识别", "天气", "空调", "其它"});
        adapter.setChoiceMode(BaseRecyclerAdapter.ChoiceMode.NONE);
        adapter.setChoiceState(BaseRecyclerAdapter.ChoiceState.NONE);
        adapter.bindHolder(new OnBindHolder<RecyclerView.ViewHolder, String>() {
            @Override
            public void onBind(RecyclerView.ViewHolder viewHolder, String s, int i) {
                TextView text = viewHolder.itemView.findViewById(R.id.text);
                text.setText(s);
            }
        });

        adapter.setOnItemClickListener(new OnItemClickListener<RecyclerView.ViewHolder, String>() {
            @Override
            public boolean onItemClick(RecyclerView.ViewHolder viewHolder, String s, int i) {
                switch (s) {
                    case "音量设置":
                        openActivity(VolumeSettingsActivity.class);
                        break;
                    case "背光设置":
                        openActivity(BrightnessLevelActivity.class);
                        break;
                    case "语音识别":
                        openActivity(SpeechRecognitionActivity.class);
                        break;
                    case "天气":
                        openActivity(WeatherActivity.class);
                        break;
                    case "空调":
                        openActivity(AirConditionActivity.class);
                        break;
                    case "其它":
                        clickOther();
                        break;
                }
                return true;
            }
        });
    }

    private void openActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    private int count = 0;

    private void clickOther() {
//        findApp("SampleAutoUI");
//        Toaster.toast("toast" + count++, 3000);

        DialogSpeechRecognition.getInstance().show();
    }

    private void installAppList() {
    }

    private PackageInfo findApp(String appName) {
        if (appName == null || appName.length() == 0) {
            return null;
        }
        try {
            PackageManager pm = getPackageManager();
            List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
            if (installedPackages.size() == 0) {
                return null;
            }
            PackageInfo pi = null;
            for (PackageInfo packageInfo : installedPackages) {
                try {
                    ApplicationInfo applicationInfo = packageInfo.applicationInfo;
                    String label = getResources().getString(applicationInfo.labelRes);
                    if (appName.equals(label)) {
                        pi = packageInfo;

                        findLaunchActivity(packageInfo.packageName);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return pi;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private void findLaunchActivity(String packageName) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setPackage(packageName);
        PackageManager pm = getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
        for (ResolveInfo resolveInfo : resolveInfos) {
            Log.d("findLaunchActivity", "LaunchActivity packageName=" + packageName + " className=" + resolveInfo.activityInfo.name);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
